//
//  LYPullDownController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/13.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYPullDownController.h"
#import "CQTopBarViewController.h"
#import "LYRecomedController.h"
#import "LYAreaController.h"
#import "LYCompanyController.h"
#import "LYClaimController.h"
#import "LYRecomendCell.h"

@interface LYPullDownController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) CQTopBarViewController *topBar;

@property (nonatomic, strong) NSArray *recomendArray;
@property (nonatomic, strong) NSArray *areaArray;
@property (nonatomic, strong) NSArray *companyArray;

@end

@implementation LYPullDownController

-(NSArray *)recomendArray{
    if (!_recomendArray) {
        _recomendArray =@[@{@"name":@"推荐",@"id":@"1"},@{@"name":@"最新",@"id":@"2"}];
    }
    return _recomendArray;
}

- (NSArray *)areaArray{
    if (!_areaArray) {
        _areaArray = @[@{@"name":@"附近",@"id":@"1",@"child":@[@{@"name":@"距离不限",@"id":@"1"},@{@"name":@"1公里",@"id":@"2"},@{@"name":@"3公里",@"id":@"3"}]},@{@"name":@"商圈",@"id":@"2",@"child":@[@{@"name":@"海淀区",@"id":@"1"},@{@"name":@"朝阳区",@"id":@"2"},@{@"name":@"通州区",@"id":@"3"}]}];
    }
    return _areaArray;
}

-(NSArray *)companyArray{
    if (!_companyArray) {
        _companyArray = @[
                          @{@"name":@"公司规模",@"id":@"1",@"child":@[@{@"name":@"少于15热",@"id":@"1"},@{@"name":@"15-20人",@"id":@"2"},@{@"name":@"20-30人",@"id":@"3"},@{@"name":@"30-40人",@"id":@"4"}]},
                          @{@"name":@"融资情况",@"id":@"2",@"child":@[@{@"name":@"未融资",@"id":@"1"},@{@"name":@"天使轮",@"id":@"2"},@{@"name":@"A轮",@"id":@"3"}]},
                          @{@"name":@"公司行业",@"id":@"3",@"child":@[@{@"name":@"区块链",@"id":@"1"},@{@"name":@"互联网",@"id":@"2"},@{@"name":@"电子商务",@"id":@"3"}]}];
    }
    return _companyArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(247, 247, 247);
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self createSubview];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

}

- (void)createSubview{
    NSString * dicStr = [[NSBundle mainBundle] pathForResource:@"data.json" ofType:nil];
    NSData *data = [[NSData alloc] initWithContentsOfFile:dicStr];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data
                                                            options:NSJSONReadingMutableContainers
                                                              error:nil];
    
    __weak typeof(self) weakSelf = self;
    self.topBar = [[CQTopBarViewController alloc] init];
    self.topBar.sectionTitles = @[@"推荐",@"区域",@"公司",@"要求"];
    //推荐
    LYRecomedController *recommendVC = [[LYRecomedController alloc] init];
    recommendVC.dataArray = self.recomendArray;
    recommendVC.block = ^(NSDictionary *dic){
        [weakSelf recomdendItemClick:dic];
    };
    
    //区域
    LYAreaController *areaVC = [[LYAreaController alloc] init];
    areaVC.dataArray = self.areaArray;
    areaVC.blcok = ^(NSDictionary *dic1,NSDictionary *dic2){
        [weakSelf areaItemClick:dic1 areaDic:dic2];
    };
    
    //公司
    LYCompanyController *companyVC = [[LYCompanyController alloc] init];
    companyVC.dataArray = self.companyArray;
    companyVC.block = ^(NSArray *array){
        [weakSelf companyItemClick:array];
    };
    
    //要求
    LYClaimController *claimVC = [[LYClaimController alloc] init];
    claimVC.dataDic = dataDic;
    
    self.topBar.pageViewClasses = @[recommendVC,areaVC,companyVC,claimVC];
    self.topBar.selectSegmentImage = @"pulldown_select";
    self.topBar.segmentImage = @"pulldown_unselect";
    self.topBar.selectedTitleTextColor = [UIColor redColor];
    self.topBar.titleTextColor = RGB(34, 34, 34);
    [self addChildViewController:self.topBar];
    [self.view addSubview:self.topBar.view];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    
    [self.tableview registerClass:[LYRecomendCell class] forCellReuseIdentifier:kCellIdentifier_LYRecomendCell];
    
    [self.topBar.footerView addSubview:self.tableview];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYRecomendCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYRecomendCell forIndexPath:indexPath];
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

#pragma mark -- Action
- (void)recomdendItemClick:(NSDictionary *)dic{
    [self.topBar topBarReplaceObjectsAtIndexes:0 withObjects:@"推荐"];
    self.title = dic[@"name"];
}

- (void)areaItemClick:(NSDictionary *)dic1  areaDic:(NSDictionary *)dic2{
    [self.topBar topBarReplaceObjectsAtIndexes:1 withObjects:@"区域"];
    self.title = [NSString stringWithFormat:@"%@-%@",dic1[@"name"],dic2[@"name"]];
}

- (void)companyItemClick:(NSArray *)array{
    [self.topBar topBarReplaceObjectsAtIndexes:2 withObjects:@"公司"];
    NSString *str = @"";
    for (NSDictionary *dic in array) {
        str = [NSString stringWithFormat:@"%@-%@",str,dic[@"name"]];
    }
    self.title = str;
}




@end
